/**
 * Created by apple on 16/11/18.
 */
var total = "";
var rows = 10;
var page_articles = 1;
var data_motorcycle = new Array();
var article_info = "";//左下角的文章信息
window.onload = function () {
    if (storage_admin_id) {
        select_change()

        $('#dataTables-example').dataTable();
        $("#sideNav").click(function () {
            if ($(this).hasClass('closed')) {
                $('.navbar-side').animate({left: '0px'});
                $(this).removeClass('closed');
                $('#page-wrapper').animate({'margin-left': '260px'});

            }
            else {
                $(this).addClass('closed');
                $('.navbar-side').animate({left: '-260px'});
                $('#page-wrapper').animate({'margin-left': '0px'});
            }
        });


    } else {
        window.location.href = "login.html"
    }
};

function articles() {
    $.ajax({
        type: 'GET',
        url: "http://118.178.59.93:8080/Motocycle/api/v1/articles/forback",
        data: data_motorcycle,
        async: false,
        success: function (data) {
            var obj = JSON.parse(data);
            try {
                if (obj.code == 0) {
                    total = obj.data.total;
                    var articles_list_total = "";
                    $.each(obj.data.rows, function (i, item) {
                        articles_list_total += "<tr>" +
                            "<td>" + item.user.nickname + "</td>" +
                            "<td style='width: 20%;'>" +
                            "<a href='article_details.html?article_id=" + item.article_id + "' target='_blank'>" + item.title + "</a>"

                        articles_list_total += "</td>";
                        //主题
                        if (item.theme_name) {
                            articles_list_total += "<td>" + item.theme_name + "</td>"
                        } else {
                            articles_list_total += "<td></td>"
                        }
                        articles_list_total += "<td>" + item.mototype_name + "</td>";
                        //自己删除
                        if (item.del_flag == 1) {
                            articles_list_total += "<td style='width: 10%;'>是</td>"
                        } else {
                            articles_list_total += "<td style='width: 10%;'></td>"
                        }
                        //管理员删除
                        if (item.admin_del == 1) {
                            articles_list_total += "<td style='width: 10%;'>是</td>"
                        } else {
                            articles_list_total += "<td style='width: 10%;'></td>"
                        }
                        articles_list_total += "<td>" + item.createdDate.substring(0, 11) + "</td>" +
                            "<td style='margin: 5px 0'>";
                        if (item.del_flag != 1 && item.admin_del != 1) {
                            articles_list_total += "<span class='operation_button' style='background: #729c5c;margin: 0;font-size: 10px;padding: 5px;'>" +
                                "<a href='carouselFigure_add.html?article_id=" + item.article_id + "' style='color: white;'>设为轮播图</a>" +
                                "</span>"
                        }
                        articles_list_total += "</td></tr>"
                    });
                    $('#dataTables-example tbody').html(articles_list_total);
                    page_articles = data_motorcycle.page;
                }
            } catch (e) {

            }
        }
    })
}

//每页展示的数目不同
function select_change() {
    $('.pageNum').remove();
    $('#articles_paginate').html('<div class="pageNum"></div>')
    rows = $("#select_change option:selected").val();
    page_articles = 1;
    data_motorcycle = {
        page: page_articles,
        rows: rows
    };
    articles();
    article_info = "当前第 1 页 , 已展示 " + rows + " 条 , 共 " + total + " 条数据";
    $('#article_info').html(article_info);
    //分页
    var _pagenum = Math.ceil(total / data_motorcycle.rows);
    if (_pagenum > 1) {
        var end_list = "";
        //需要分页展示
        $('.pageNum').show();
        $('.pageNum').createPage({
            pageCount: _pagenum,
            current: data_motorcycle.page,
            backFn: function (p) {
                //单击回调方法，p是当前页码
                data_motorcycle.page = p;
                end_list = p * data_motorcycle.rows;
                articles();
                article_info = "当前第 " + p + " 页 , 已展示 " + end_list + " 条 , 共 " + total + " 条数据";
                $('#article_info').html(article_info)
            }
        });
    }
    $('.loading').hide()
}

